Imports System
Imports System.Drawing
Imports C1.Win.C1FlexGrid
Imports System.Windows.Forms

<ToolboxBitmap(GetType(ToolTip))> _
Public Class ACCMSSeleccionarC1
   Inherits ContextMenuStrip

   Friend WithEvents cmsMarcarTodos As System.Windows.Forms.ToolStripMenuItem
   Friend WithEvents cmsMarcarSeleccionados As System.Windows.Forms.ToolStripMenuItem
   Friend WithEvents cmsDesmarcarTodos As System.Windows.Forms.ToolStripMenuItem
   Friend WithEvents cmsDesmarcarSeleccionados As System.Windows.Forms.ToolStripMenuItem
   Public Event ACClick(ByVal sender As ACSeleccionarC1, ByVal e As EventArgs)

   Public Enum ACSeleccionarC1
      MarcarTodos
      MarcarSeleccionados
      DesmarcarTodos
      DesmarcarSeleccionados
   End Enum

   Public Sub New()
      MyBase.New()

      '
      'cmsMarcarTodos
      '
      Me.cmsMarcarTodos = New System.Windows.Forms.ToolStripMenuItem
      Me.cmsMarcarTodos.Name = "cmsMarcarTodos"
      Me.cmsMarcarTodos.Size = New System.Drawing.Size(206, 22)
      Me.cmsMarcarTodos.Text = "Marcar Todos"
      '
      'cmsMarcarSeleccionados
      '
      Me.cmsMarcarSeleccionados = New System.Windows.Forms.ToolStripMenuItem
      Me.cmsMarcarSeleccionados.Name = "cmsMarcarSeleccionados"
      Me.cmsMarcarSeleccionados.Size = New System.Drawing.Size(206, 22)
      Me.cmsMarcarSeleccionados.Text = "Marcar Seleccionados"
      '
      'cmsDesmarcarTodos
      '
      Me.cmsDesmarcarTodos = New System.Windows.Forms.ToolStripMenuItem
      Me.cmsDesmarcarTodos.Name = "cmsDesmarcarTodos"
      Me.cmsDesmarcarTodos.Size = New System.Drawing.Size(206, 22)
      Me.cmsDesmarcarTodos.Text = "Desmarcar Todos"
      '
      'cmsDesmarcarSeleccionados
      '
      Me.cmsDesmarcarSeleccionados = New System.Windows.Forms.ToolStripMenuItem
      Me.cmsDesmarcarSeleccionados.Name = "cmsDesmarcarSeleccionados"
      Me.cmsDesmarcarSeleccionados.Size = New System.Drawing.Size(206, 22)
      Me.cmsDesmarcarSeleccionados.Text = "Desmarcar Seleccionados"
      '
      'cmsOpciones
      '
      Me.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.cmsMarcarTodos, Me.cmsMarcarSeleccionados, Me.cmsDesmarcarTodos, Me.cmsDesmarcarSeleccionados})
      Me.Name = "cmsOpciones"
      Me.Size = New System.Drawing.Size(207, 92)
   End Sub

   Private m_grilla As Object
   Private m_columna As String

   Public Sub ACMostrar(ByRef x_grilla As Object, ByVal x_posicion As Point, Optional ByVal x_columna As String = "Seleccionar")
      Try
         m_grilla = x_grilla
         m_columna = x_columna
         Me.Show(x_posicion)
      Catch ex As Exception

      End Try
   End Sub

   Private Sub cmsMarcarTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsMarcarTodos.Click
      Seleccionar(ACSeleccionarC1.MarcarTodos)
      RaiseEvent ACClick(ACSeleccionarC1.MarcarTodos, e)
   End Sub
   Private Sub cmsMarcarSeleccionados_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsMarcarSeleccionados.Click
      Seleccionar(ACSeleccionarC1.MarcarSeleccionados)
      RaiseEvent ACClick(ACSeleccionarC1.MarcarSeleccionados, e)
   End Sub
   Private Sub cmsDesmarcarTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsDesmarcarTodos.Click
      Seleccionar(ACSeleccionarC1.DesmarcarTodos)
      RaiseEvent ACClick(ACSeleccionarC1.DesmarcarTodos, e)
   End Sub
   Private Sub cmsDesmarcarSeleccionados_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsDesmarcarSeleccionados.Click
      Seleccionar(ACSeleccionarC1.DesmarcarSeleccionados)
      RaiseEvent ACClick(ACSeleccionarC1.DesmarcarSeleccionados, e)
   End Sub



   Private Sub Seleccionar(ByVal x_seleccion As ACSeleccionarC1)
      Select Case m_grilla.GetType().ToString()
         Case "System.Windows.Forms.DataGridView"
            SeleccionarDataGridView(x_seleccion, m_grilla, m_columna)
         Case "C1.Win.C1FlexGrid.C1FlexGrid"
            SeleccionarC1FlexGrid(x_seleccion, m_grilla, m_columna)
      End Select
   End Sub

   Private Sub SeleccionarDataGridView(ByVal x_seleccion As ACSeleccionarC1, ByRef x_dataGridView As DataGridView, Optional ByVal x_columna As String = "Seleccionar")
      Select Case x_seleccion
         Case ACSeleccionarC1.MarcarTodos
            For Each datagridrow As DataGridViewRow In x_dataGridView.Rows
               datagridrow.Cells(x_columna).Value = True
            Next
         Case ACSeleccionarC1.MarcarSeleccionados
            For Each datagridrow As DataGridViewRow In x_dataGridView.SelectedRows
               datagridrow.Cells(x_columna).Value = True
            Next
         Case ACSeleccionarC1.DesmarcarTodos
            For Each datagridrow As DataGridViewRow In x_dataGridView.Rows
               datagridrow.Cells(x_columna).Value = False
            Next
         Case ACSeleccionarC1.DesmarcarSeleccionados
            For Each datagridrow As DataGridViewRow In x_dataGridView.SelectedRows
               datagridrow.Cells(x_columna).Value = False
            Next
      End Select
   End Sub
   Private Sub SeleccionarC1FlexGrid(ByVal x_seleccion As ACSeleccionarC1, ByRef x_flexGrid As C1FlexGrid, Optional ByVal x_columna As String = "Seleccionar")
      Select Case x_seleccion
         Case ACSeleccionarC1.MarcarTodos
            For Each fila As C1.Win.C1FlexGrid.Row In x_flexGrid.Rows
               If fila.Index > 0 Then
                  fila(x_columna) = True

               End If
            Next
         Case ACSeleccionarC1.MarcarSeleccionados
            Dim index_ini As Integer = x_flexGrid.Selection.r1
            Dim index_fin As Integer = x_flexGrid.Selection.r2

            For index As Integer = index_ini To index_fin
               x_flexGrid.Rows(index)(x_columna) = True
            Next
         Case ACSeleccionarC1.DesmarcarTodos
            For Each fila As C1.Win.C1FlexGrid.Row In x_flexGrid.Rows
               If fila.Index > 0 Then
                  fila(x_columna) = False
               End If
            Next
         Case ACSeleccionarC1.DesmarcarSeleccionados
            Dim index_ini As Integer = x_flexGrid.Selection.r1
            Dim index_fin As Integer = x_flexGrid.Selection.r2

            For index As Integer = index_ini To index_fin
               x_flexGrid.Rows(index)(x_columna) = False
            Next
      End Select
   End Sub

End Class